/*
 * Copyright 2011 Clean GWT
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.cleanform.gwt.chinesechess.client.comet;

import java.util.List;

import com.cleanform.gwt.chinesechess.client.comet.ServerPushEvent.ServerPushEventHandler;
import com.cleanform.gwt.chinesechess.shared.comet.UserEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.HasHandlers;

/**
 * Represents a logic event of Comet.
 * 
 * @author Jess
 * @version 1.0
 * @since 1.0
 */
public class ServerPushEvent extends GwtEvent<ServerPushEventHandler> {
	/**
	 * Handler interface for {@link ServerPushEvent} events.
	 */
	public static interface ServerPushEventHandler extends EventHandler {
		/**
		 * Called when a {@link ServerPushEvent} is fired.
		 * 
		 * @param event the {@link ServerPushEvent} that was fired
		 */
		void onServerPush(ServerPushEvent event);
	}

	/**
	 * Interface specifying that a class can add {@code CometHandler}s.
	 */
	public interface HasCometHandlers extends HasHandlers {
		/**
		 * Adds a {@link ServerPushEvent} handler.
		 * 
		 * @param handler the handler
		 * @return {@link HandlerRegistration} to remove this handler
		 */
		HandlerRegistration addServerPushHandler(ServerPushEventHandler handler);
	}

	/**
	 * Fires an Comet event on all registered handlers in the handler manager.
	 * If no such handlers exist or no specified process for the Comet, it will
	 * do nothing.
	 * 
	 * @param source the source of the handlers
	 * @param event the Comet Comet
	 */
	public static <T> void fire(HasCometHandlers source, ServerPushEvent event) {
		if (TYPE != null) {
			source.fireEvent(event);
		}
	}

	private List<UserEvent> value;

	/**
	 * Handler type.
	 */
	private static Type<ServerPushEventHandler> TYPE;

	/**
	 * Fires an Comet event on all registered handlers in the handler manager.
	 * If no such handlers exist or no specified process for the Comet, it will
	 * do nothing.
	 * 
	 * @param source the source of the handlers
	 * @param Comet the Comet
	 */
	public static <T> void fire(HasCometHandlers source, List<UserEvent> Comet) {
		fire(source, new ServerPushEvent(Comet));
	}

	public static Type<ServerPushEventHandler> getType() {
		if (TYPE == null) {
			TYPE = new Type<ServerPushEventHandler>();
		}
		return TYPE;
	}

	/**
	 * Creates a Comet event.
	 * 
	 * @param Comet
	 */
	protected ServerPushEvent(List<UserEvent> value) {
		this.value = value;
	}

	/**
	 * Gets the type associated with this event.
	 * 
	 * @return returns the handler type
	 */
	@Override
	public Type<ServerPushEventHandler> getAssociatedType() {
		return ServerPushEvent.TYPE;
	}

	/**
	 * @return the id of logic
	 */
	public List<UserEvent> getComet() {
		return value;
	}

	@Override
	protected void dispatch(ServerPushEventHandler handler) {
		handler.onServerPush(this);
	}

}
